package com.catazy.mvvmlib.widgets

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
import android.view.View.OnFocusChangeListener
import android.view.animation.Animation
import android.view.animation.CycleInterpolator
import android.view.animation.TranslateAnimation
import androidx.appcompat.widget.AppCompatEditText
import androidx.core.content.ContextCompat

/**
 * 作者: Tao
 * 日期: 2024/1/31
 * 时间: 16:05
 * 描述: 带清除按钮的EditTextView
 */
class ClearEditText(
    context: Context,
    attrs: AttributeSet?,
    defStyle: Int = android.R.attr.editTextStyle
) : AppCompatEditText(context, attrs, defStyle), OnFocusChangeListener, TextWatcher {

    //删除按钮的引用
    private var mClearDrawable: Drawable? = null

    //控件是否有焦点
    private var mHasFocus = false

    init {
        //获取EditText的DrawableRight,假如没有设置我们就使用默认的图片
        mClearDrawable = compoundDrawables[2]
        if (mClearDrawable == null) {
            throw NullPointerException("You can add drawableRight attribute in XML");
//            mClearDrawable = ContextCompat.getDrawable(context, R.mipmap.ic_delete_gray)
        }
        mClearDrawable?.setBounds(
            0,
            0,
            mClearDrawable?.intrinsicWidth ?: 0,
            mClearDrawable?.intrinsicHeight ?: 0
        )
        //默认设置隐藏图标
        setClearIconVisible(false)
        //设置焦点改变监听
        onFocusChangeListener = this
        //设置输入框里面内容发生改变的监听
        addTextChangedListener(this)
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (event.action == MotionEvent.ACTION_UP) {
            if (compoundDrawables[2] != null) {
                val touchable =
                    event.x > width - totalPaddingRight && event.x < width - paddingRight
                if (touchable) {
                    this.setText("")
                }
            }
        }
        return super.onTouchEvent(event)
    }

    override fun onFocusChange(v: View?, hasFocus: Boolean) {
        mHasFocus = hasFocus
        if (hasFocus) {
            setClearIconVisible(text!!.isNotEmpty())
        } else {
            setClearIconVisible(false)
        }
    }

    override fun onTextChanged(text: CharSequence, start: Int, before: Int, count: Int) {
        if (mHasFocus) {
            //获取焦点设置回之前的图标
            mClearDrawable = compoundDrawables[2]
            mClearDrawable?.setBounds(
                0,
                0,
                mClearDrawable?.intrinsicWidth ?: 0,
                mClearDrawable?.intrinsicHeight ?: 0
            )
            setClearIconVisible(text.isNotEmpty())
        }
    }

    //
    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

    override fun afterTextChanged(s: Editable?) {}

    /**
     * 设置清除图片的显示与隐藏，调用setCompoundDrawables为EditText绘制上去
     *
     * @param visible
     */
    fun setClearIconVisible(visible: Boolean) {
        //判断是否输入了内容,如果输入了内容我们就把弹窗关闭
        val right = if (visible) mClearDrawable else null
        setCompoundDrawables(
            compoundDrawables[0],
            compoundDrawables[1],
            right,
            compoundDrawables[3]
        )
    }

    /**
     * 设置晃动动画
     */
    fun setShakeAnimation() {
        this.animation = shakeAnimation(5)
    }

    /**
     * 晃动动画
     *
     * @param counts 1秒晃动多少下
     * @return
     */
    private fun shakeAnimation(counts: Int): Animation {
        val trAnimation: Animation = TranslateAnimation(0f, 10f, 0f, 0f)
        trAnimation.interpolator = CycleInterpolator(counts.toFloat())
        trAnimation.duration = 1000
        return trAnimation
    }
}